package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.SysDictDO;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.service.MetaDataService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

 
@Controller
@RequestMapping("/sys/meta")
@Api(value = "/sys/meta", description = "元数据操作接口")
public class MetaDataController  extends BaseController {
	@Autowired
	private MetaDataService metaDataService;
	
	
	@ResponseBody
	@GetMapping("/list")
	@ApiOperation(value="元数据查询", notes="根据条件查询元数据", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "元数据id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "parentId", value = "元数据上级id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "name", value = "标签名", required = false,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "type", value = "类型值", required = false,paramType="query", dataType = "String"),
        @ApiImplicitParam(name = "description", value = "类型描述", required = false,paramType="query",  dataType = "String"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,paramType="query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true,paramType="query", dataType = "Integer")
	})
	public R list(@RequestParam Map<String, Object> params) throws Exception{
		//查询列表数据
        Query query = new Query(params);
		List<SysDictDO> sysDictList = metaDataService.list(query);
		PageInfo<SysDictDO> pageInfo = new PageInfo<SysDictDO>(sysDictList);
		int total = (int)pageInfo.getTotal();
		PageUtils pageUtils = new PageUtils(sysDictList, total);
		R r = new R();
		r.put("list", pageUtils);
		return r;
	}

	
	@GetMapping( "/type")
	@ResponseBody
	public List<SysDictDO> listType() throws Exception{
		return metaDataService.listType();
	};
	
	@ResponseBody
	@GetMapping("/edit")
	@ApiOperation(value="元数据查询", notes="根据条件查询元数据", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "元数据id", required = false,paramType="query", dataType = "Long")
	})
	R edit(Long id) throws Exception{
		SysDictDO sysDict = metaDataService.get(id);
		R r = new R();
		r.put("obj", sysDict);
	    return r;
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@ApiOperation(value="数据字典保存", notes="", response=String.class)
	public R save(@RequestBody SysDictDO sysDict){
		try {
			if(sysDict.getParentId() == null) {
				sysDict.setParentId(0L);
			}
			if(metaDataService.save(sysDict)>0){
				return R.saveOk(sysDict.getId()+"");
			}
			return R.error();
		}catch(Exception e) {
			e.printStackTrace();
			return R.error();	
		}
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@PostMapping("/update")
	@ApiOperation(value="数据字典保存", notes="", response=String.class)
	public R update(@RequestBody SysDictDO sysDict){
		try {
			metaDataService.update(sysDict);
			return R.ok();
		}catch(Exception e) {
			e.printStackTrace();
			return R.error();
		}
	}
	
	/**
	 * 删除
	 */
	@GetMapping( "/remove")
	@ResponseBody
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "元数据id", required = false,paramType="query", dataType = "Long")
	})
	@ApiOperation(value="元数据删除", notes="根据条件删除元数据", response=String.class)
	public R remove( Long id) throws Exception{
		if(metaDataService.remove(id)>0){
			return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Long[] ids) throws Exception{
		metaDataService.batchRemove(ids);
		return R.ok();
	}
	
	//类别已经指定增加
	@GetMapping("/add/{type}/{description}")
	String addD( Model model,@PathVariable("type") String type, @PathVariable("description") String description ) throws Exception{
		model.addAttribute("type", type);
		model.addAttribute("description",description);
	    return "common/sysDict/add";
	}
	
}
